package com.ruoyi.web.controller.bookstore;

import com.ruoyi.bookstore.domain.BorrowCard;
import com.ruoyi.bookstore.service.IBorrowCardService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 借阅证Controller
 * 
 * @author ruoyi
 * @date 2022-07-20
 */
@RestController
@RequestMapping("/bookstore/card")
public class BorrowCardController extends BaseController
{
    @Autowired
    private IBorrowCardService borrowCardService;

    /**
     * 查询借阅证列表
     */
    @PreAuthorize("@ss.hasPermi('bookstore:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(BorrowCard borrowCard)
    {
        startPage();
        List<BorrowCard> list = borrowCardService.selectBorrowCardList(borrowCard);
        return getDataTable(list);
    }

    /**
     * 导出借阅证列表
     */
    @PreAuthorize("@ss.hasPermi('bookstore:card:export')")
    @Log(title = "借阅证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BorrowCard borrowCard)
    {
        List<BorrowCard> list = borrowCardService.selectBorrowCardList(borrowCard);
        ExcelUtil<BorrowCard> util = new ExcelUtil<BorrowCard>(BorrowCard.class);
        util.exportExcel(response, list, "借阅证数据");
    }

    /**
     * 获取借阅证详细信息
     */
    @PreAuthorize("@ss.hasPermi('bookstore:card:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(borrowCardService.selectBorrowCardById(id));
    }

    /**
     * 新增借阅证
     */
    @PreAuthorize("@ss.hasPermi('bookstore:card:add')")
    @Log(title = "借阅证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BorrowCard borrowCard)
    {
        return toAjax(borrowCardService.insertBorrowCard(borrowCard));
    }

    /**
     * 修改借阅证
     */
    @PreAuthorize("@ss.hasPermi('bookstore:card:edit')")
    @Log(title = "借阅证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BorrowCard borrowCard)
    {
        return toAjax(borrowCardService.updateBorrowCard(borrowCard));
    }

    /**
     * 删除借阅证
     */
    @PreAuthorize("@ss.hasPermi('bookstore:card:remove')")
    @Log(title = "借阅证", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(borrowCardService.deleteBorrowCardByIds(ids));
    }
}
